<?php
    $action=$_GET['action'];
	$licesneID=$_GET['licesneID'];
	$type=$_GET['type'];
	$updateID=($_GET['updateID'])*100;
	if($updateID<100){
	  $updateID=100;
	}
	//将updateID*100就可以和以前的一模一样了
	$relmove=$updateID+1;
	$connect=mysql_connect("localhost","root","");
    mysql_select_db("shenji",$connect);
	
    //根据type找到type表中的beizhu(英文找到中文)
	$sql="select * from type where beizhu='$type'";
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
    $type=$row['name'];
	
	
	$array=array();

	if($action==read){
		
		$sql="select * from jiekou where licesneid='".$licesneID."'";
	    $result=mysql_query($sql);
		$row=mysql_fetch_array($result);
		$jid=$row['id'];
		$status=$row['status'];
		if(isset($row['id'])){
	       $sql="select * from shenji where move=$relmove and type='".$type."'";
	       $result=mysql_query($sql);
	       $row=mysql_fetch_array($result);
	       $version=$row['version'];
		   $id=$row['id'];
		   $publish_jiqi=$row['publish_jiqi'];#开关
		   $jiekou_id=$row['jiekou_id'];
		     $sql2="select max(move) from shenji where type='".$type."'";
			 $result2=mysql_query($sql2);
	         $row2=mysql_fetch_array($result2);
			 $max_move=$row2[0];
			 if($updateID<$max_move){
				 
			   $txt = sprintf("%1\$.2f",$version*0.01);
			   $file_name=$txt."_update.tar.gz";
               $file_dir="/var/www/html/upload/upload/".$type."/";
			    if (! file_exists ( $file_dir . $file_name )){    
				       echo "文件不存在";
                       exit ();    
                 } else{
					 $flag=0;
					 //publish:1:关闭  0:开启   status:1:开启  0:关闭     
    				 
					 if($publish_jiqi==1){
						 if($jiekou_id==$jid){  //关闭状态只有是本身才能下载
						     if($status==1){    //本身要是开启状态
								 $flag=1;
							 }
						 }
						 
					 }else{                      //开启状态
						 if($status==1){         //只要该机器码开启就行了
							 $flag=1;
						 }
					 }
					 
					 
					 if($flag){
						  //$size=filesize($file_dir.$file_name);
					/* 	  $file = fopen ( $file_dir . $file_name, "r" );    
						  Header ( "Content-type: application/octet-stream" );    
						  Header ( "Accept-Ranges: bytes" );    
						  Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );    
						  Header ( "Content-Disposition: attachment; filename=$file_name" );         
						  echo fread ( $file, filesize ( $file_dir . $file_name ) );    
						  fclose ( $file );    
						  exit (); */
					 	header('content-type:application/octet-stream');
						header('accept-ranges: bytes');
						header('content-length: '.filesize($file_dir.$file_name));
						//header('Content-length: ' . $size);
						header('content-disposition:attachment;filename='.$file_name);
						$fp = fopen($file_dir.$file_name, "r");
						while(!feof($fp)) {
						echo fgets($fp,4096);
						}
						fclose($fp);
						   
					 }else{
						 header('HTTP/1.1 403 Forbidden');
		                 header("status: 403 Forbidden");
						 $array["status"]=-1;
						 $array["xingxi"]=urlencode("权限被禁止!");
						 $json=json_encode($array);
						 echo urldecode($json);
					 }
					 
				 }
				 
			 }else{
				
				  header('http/1.1 404 Not Found');
                  header("status: 404 Not Found"); 
				  $array["status"]=-1;
				  $array["xingxi"]=urlencode("没有更新的版本或类型输入错误!");
				  $json=json_encode($array);
				  echo urldecode($json);
				  
			 }
			 
	   }else{
		   header('HTTP/1.1 403 Forbidden');
		   header("status: 403 Forbidden");
		   if($licesneID!=null){
			   //将licesneID添加数据库
			   $ip=$_SERVER["REMOTE_ADDR"];
			   $beizhu=$licesneID.$ip;
			   $sql="insert into other_jiekou(licesneid,beizhu) value('$licesneID','$beizhu')";
			   mysql_query($sql);
			   $array["status"]=-1;
			   $array["xingxi"]=urlencode("licesneID现在已存入");
			   $json=json_encode($array);
			   echo urldecode($json);
		   }else{
			   $array["status"]=-1;
			   $array["xingxi"]=urlencode("请输入licesneID的值");
			   $json=json_encode($array);
			   echo urldecode($json);
			   
		   }
		   
		  
	   }
    }else{
	   header('HTTP/1.1 403 Forbidden');
	   header("status: 403 Forbidden");
	   $array["status"]=-1;
	   $array["xingxi"]=urlencode("输入的action值有误!");
	   $json=json_encode($array);
	   echo urldecode($json);
    }
?>